package com.ruoyi.project.online_experiment.service.impl;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;

import com.ruoyi.framework.web.service.DictService;
import com.ruoyi.project.online_experiment.domain.Echart;
import com.ruoyi.project.online_experiment.domain.OnlineExperiment;
import com.ruoyi.project.online_experiment.mapper.OnlineExperimentMapper;
import com.ruoyi.project.online_experiment.service.IOnlineExperimentService;
import com.ruoyi.project.online_experimentStu.mapper.OnlineExperimentStuMapper;
import com.ruoyi.project.system.dict.domain.DictData;
import com.ruoyi.project.system.dict.domain.DictType;
import com.ruoyi.project.system.dict.service.IDictTypeService;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ruoyi.common.utils.text.Convert;

/**
 * 【请填写功能名称】Service业务层处理
 * 
 * @author ruoyi
 * @date 2020-04-14
 */
@Service
public class OnlineExperimentServiceImpl implements IOnlineExperimentService
{
    @Autowired
    private OnlineExperimentMapper onlineExperimentMapper;

    @Autowired
    private OnlineExperimentStuMapper onlineExperimentStuMapper;

    /**
     * 查询【请填写功能名称】
     * 
     * @param onlineExperimentId 【请填写功能名称】ID
     * @return 【请填写功能名称】
     */
    @Override
    public OnlineExperiment selectOnlineExperimentById(Long onlineExperimentId)
    {
        return onlineExperimentMapper.selectOnlineExperimentById(onlineExperimentId);
    }

    /**
     * 查询【请填写功能名称】列表
     * 
     * @param onlineExperiment 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<OnlineExperiment> selectOnlineExperimentList(OnlineExperiment onlineExperiment)
    {
        return onlineExperimentMapper.selectOnlineExperimentList(onlineExperiment);
    }

    /**
     * 新增【请填写功能名称】
     * 
     * @param onlineExperiment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertOnlineExperiment(OnlineExperiment onlineExperiment)
    {
        return onlineExperimentMapper.insertOnlineExperiment(onlineExperiment);
    }

    /**
     * 修改【请填写功能名称】
     * 
     * @param onlineExperiment 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateOnlineExperiment(OnlineExperiment onlineExperiment)
    {
        return onlineExperimentMapper.updateOnlineExperiment(onlineExperiment);
    }

    /**
     * 删除【请填写功能名称】对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOnlineExperimentByIds(String ids)
    {
        //删除实验项目之前删除教师布置的实验和学生提交的结果
        String[] sArr=Convert.toStrArray(ids);
        for(String id:sArr){
            //根据id去获取教师布置的实验id
            List<String> lists=onlineExperimentMapper.getAllExperiemntTea(Long.valueOf(id));
            //根据教师布置的实验id去删除布置的实验即提交实验的内容
            for(String list:lists){
                //删除学生提交的信息
                onlineExperimentStuMapper.deleteOnlineExperimentStu(list);
                //删除教室布置的实验信息
                onlineExperimentStuMapper.deleteOnlineExperimentTea(list);
            }
        }
        return onlineExperimentMapper.deleteOnlineExperimentByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除【请填写功能名称】信息
     * 
     * @param onlineExperimentId 【请填写功能名称】ID
     * @return 结果
     */
    @Override
    public int deleteOnlineExperimentById(Long onlineExperimentId)
    {
        //根据id去获取教师布置的实验id
        List<String> lists=onlineExperimentMapper.getAllExperiemntTea(Long.valueOf(onlineExperimentId));
        //根据教师布置的实验id去删除布置的实验即提交实验的内容
        for(String list:lists){
            //删除学生提交的信息
            onlineExperimentStuMapper.deleteOnlineExperimentStu(list);
            //删除教室布置的实验信息
            onlineExperimentStuMapper.deleteOnlineExperimentTea(list);
        }
        return onlineExperimentMapper.deleteOnlineExperimentById(onlineExperimentId);
    }


    /**
     * 获取专业下所有的教师身份
     * @param user
     * @return
     */
    @Override
    public List<User> getAllUser(User user) {
        return onlineExperimentMapper.getAllUser(user);
    }



    @Autowired
    private DictService dictService;

    /**
     *
     * @return
     */
    @Override
    public Echart getAllEachart() {
        //創建Echart對象
        Echart echart=new Echart();
        //获取对应的专业
        List<DictData> list=dictService.getType("sys_profession");
        echart.setDataList(list);
        //存放項目的數量
        ArrayList<Integer> list1=new ArrayList<>();
        for(DictData a:list){
            //根据专业去获取项目的数量
            int count=onlineExperimentMapper.getAllOnlineExperimentCount(a.getDictValue());
            list1.add(count);
        }
        echart.setLists2(list1);
        return echart;
    }

    /**
     * 获取所有项目对应的数量
     * @return
     */
    @Override
    public List<Echart> getAllCount() {
        return onlineExperimentMapper.getAllCount();
    }

    /**
     * 获取项目对应的参与人数，即那些学生做这个项目
     * @return
     */
    @Override
    public List<Echart> getPersonCount() {
        return onlineExperimentMapper.getPersonCount();
    }
}
